//
//  ViewController.m
//  大转盘
//
//  Created by Annie on 2020/6/1.
//  Copyright © 2020 Annie. All rights reserved.
//

#import "ViewController.h"
#import "FMAwardsModel.h"
#import "FMAwardsView.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) NSMutableArray<FMAwardsModel *> *awards;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUpUI];
}

- (NSMutableArray<FMAwardsModel *> *)awards{
    if (!_awards) {
        _awards = [NSMutableArray arrayWithCapacity:9];
        
        
        for (NSInteger i = 0; i<9; i++) {
            FMAwardsModel *model = [FMAwardsModel new];
            model.title = [NSString stringWithFormat:@"%ld",i + 1];
            [_awards addObject:model];
        }
        CGFloat per_angle = M_PI * 2 / _awards.count;
        
        [_awards enumerateObjectsUsingBlock:^(FMAwardsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.angle = per_angle;
            obj.radius = 100;
//            if (idx == 1) {
//                obj.angle = per_angle * 1.5;
//            }else if (idx == 2) {
//                obj.angle = per_angle * 0.5;
//            }
            
            
            
        }];
    }
    return _awards;
}


- (void)setUpUI{
    
    [self.view addSubview:self.bgView];
    self.bgView.frame = CGRectMake(0, 0, 200, 200);
    self.bgView.center = self.view.center;
    
    
    
    
    CGFloat angleOffset = - M_PI_2;
    CGFloat angle = 0;
    for (NSInteger i = 0; i<self.awards.count; i++) {
        FMAwardsModel *model = self.awards[i];
        FMAwardsView *awardsView = [FMAwardsView new];
        awardsView.model = model;
        [self.bgView addSubview:awardsView];
        
        CGSize size = CGSizeMake(model.radius, model.radius * sin(model.angle * 0.5) * 2);
        awardsView.layer.frame = CGRectMake(0, 0 , size.width, size.height);
        awardsView.layer.anchorPoint = CGPointMake(0, 0.5);
        awardsView.layer.position = CGPointMake(model.radius, model.radius);
        awardsView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angleOffset + angle);
        angle += model.angle;
//        break;
    }
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
//    NSInteger randomNum = arc4random()%100;//控制概率
    NSInteger turnsNum = arc4random()%5+4;//控制圈数
    NSInteger turnAngle = arc4random()%9;//8个奖励分别对应的角度
       
   CABasicAnimation* rotationAnimation;
   rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2 * turnsNum + self.awards.firstObject.angle * turnAngle];
   rotationAnimation.duration = 3.0f;
   rotationAnimation.cumulative = YES;
//   rotationAnimation.delegate = self;
   //由快变慢
   rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
   rotationAnimation.fillMode=kCAFillModeForwards;
   rotationAnimation.removedOnCompletion = NO;
   [self.bgView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
}





- (UIImageView *)bgView{
    if (!_bgView) {
        UIImageView *view = [UIImageView new];
        view.layer.cornerRadius = 100;
        view.backgroundColor = [UIColor yellowColor];
        view.layer.borderWidth = 1;
        view.layer.borderColor = [UIColor redColor].CGColor;
        
        _bgView = view;
    }
    return _bgView;
}


@end
